<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>记录</title>
</head>
<body>  
<%
	String createTime = request.getParameter("createTime");
	String amount = request.getParameter("amount");
	int amountMoney = Integer.parseInt(amount);
	String type = request.getParameter("type");
	String category = request.getParameter("category");
	session.setAttribute("createTime", createTime);
	session.setAttribute("amount", amount);
	session.setAttribute("type", type);
	session.setAttribute("category", category);
	String url = "jdbc:mysql://localhost:3306/wacai?serverTimezone=Asia/Shanghai";
	Connection connection = null;
	PreparedStatement pstmt = null;
	String sql = null;
	
	if(createTime!=null && amount!=null){
		try{
			Class.forName("com.mysql.jdbc.Driver");
		}catch(ClassNotFoundException exception){
			out.println("加载驱动器类时出现异常");
		}
		
		try{
			connection = DriverManager.getConnection(url,"root","root1234");//连接数据库
			sql = "insert into record values(?,?,?,?)";
			pstmt = connection.prepareStatement(sql);
			pstmt.setString(1,createTime);
			pstmt.setInt(2,amountMoney);
			pstmt.setString(3,type);
			pstmt.setString(4,category);
			pstmt.executeUpdate();
			pstmt.close();
			connection.close();
			out.print("<script type='text/javascript'>alert('记账成功');</script>");
			response.setHeader("Refresh","0.1;url=recordShow.jsp");
		%>			
		<%	
			}catch(Exception e){
				out.print("<script type='text/javascript'>alert('保存失败');</script>");
				response.setHeader("Refresh","0.1;url=home.jsp");
	}}%>
</body>
<script type="text/javascript">
	window.onload = function(){
		document.getElementById("balanceMoney").style.display = "block";
	}
</script>
</html>